package com.songlanyun.modules.chain.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.songlanyun.common.enums.ChainConstant;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserIdentityAssetEntity;
import com.songlanyun.modules.account.service.CommonService;
import com.songlanyun.modules.chain.model.dto.UserWithdrawPreviewDTO;
import com.songlanyun.modules.chain.model.dto.UserWithdrawalDTO;
import com.songlanyun.modules.chain.service.ChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 前端用户提现、查询账户信息操作
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/api/chain/")
public class ChainApiController extends AbstractApiController {
    @Resource
    ChainService chainService;

    @Resource
    CommonService commonService;


    /**
     * 获取短信验证码
     * @return
     */
    @GetMapping(value = "/getVerfityCode")
    public R getVerfityCode() {
        AccountEntity accountEntity = chainService.getAccountAddress(getUserId(false));
        commonService.getVerfityCode(accountEntity.getMobile());
        return R.ok();
    }

    /**
     * 获取用户区块链账户地址信息
     *
     * @return 账户信息
     */
    @GetMapping(value = "/accountAddress")
    public R getAccountAddress() {
        AccountEntity accountEntity = chainService.getAccountAddress(getUserId(false));
        return R.ok().put("data", accountEntity);
//        return R.error("该功能已关闭");
    }

    /**
     * 获取账户资产信息
     *
     * @return
     */
    @GetMapping(value = "/accountIdentityAsset")
    public R getAccountIdentityAsset() {
        UserIdentityAssetEntity userIdentityAssetEntity = chainService.getUserIdentityAsset(getUserId(false));
        return R.ok().put("data", userIdentityAssetEntity);
    }

    /**
     * 前端用户提现手续费
     *
     * @return
     */
    @GetMapping(value = "/handleFee/{value}")
    public R preview(@PathVariable BigDecimal value) {
        UserWithdrawPreviewDTO userWithdrawPreviewDTO = chainService.preview(value);
        return R.ok().put("data", userWithdrawPreviewDTO);
    }

    /**
     * 前端用户提现操作
     *
     * @param userWithdrawalDTO
     * @return
     */
    @PostMapping(value = "/withdrawal")
    public R createWithdrawal(@RequestBody UserWithdrawalDTO userWithdrawalDTO) {
        ValidatorUtils.validateEntity(userWithdrawalDTO);
        AccountEntity accountEntity = chainService.getAccountAddress(getUserId(false));
        commonService.checkVerCode(accountEntity.getMobile(), userWithdrawalDTO.getVerfityCode());
        chainService.withdrawal(userWithdrawalDTO, getUserId(true));
        return R.ok();
//        return R.error("该功能已关闭");
    }

    /**
     * 获取字典码
     *
     * @param type
     * @return
     */
    @GetMapping(value = "/select/{type}")
    public R createWithdrawal(@PathVariable String type) {
        List<JSONObject> list = new ArrayList<>();
        if ("transfer_net".equals(type)) {
            ChainConstant.ChainTransferNet[] values = ChainConstant.ChainTransferNet.values();
            for (ChainConstant.ChainTransferNet value : values) {
                JSONObject object = new JSONObject();
                object.put("key", value.getCode());
                object.put("value", value.getMsg());
                list.add(object);
            }
        }
        if ("pay_type".equals(type)) {
            ChainConstant.ChainPayType[] values = ChainConstant.ChainPayType.values();
            for (ChainConstant.ChainPayType value : values) {
                JSONObject object = new JSONObject();
                object.put("key", value.getCode());
                object.put("value", value.getMsg());
                list.add(object);
            }
        }
        return R.ok().put("data", list);
    }


}
